<template>
  <el-form :disabled="disabled" ref="form" class="edit-main-box" :model="formData" :rules="rules" label-position="right"
    label-width="100px">
   <el-form-item label="类型" prop="type">
      <el-radio v-model="formData.type" label="img" 
      @change="imgs2 = []"
        >图片</el-radio
      >
      <el-radio v-model="formData.type" label="video"
      @change="imgs3 = ''"
        >视频</el-radio
      >
      <el-radio
        v-model="formData.type"
        label="link"
        @change="
          () => {
            imgs2 = [];
            imgs3 = ''
          }
        "
        >链接</el-radio
      >
    </el-form-item>
    <el-row>
      <el-col :span="12">
        <el-form-item label="封面" prop="img">
          <Upload :disabled="disabled" :file-list.sync="imgs" :limit="1" dir-name="/banner" list-type="img"
            @change="setUrl(formData, 'imgs', 'img')" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item v-if="formData.type === 'img'" label="大图" prop="link">
          <Upload :disabled="disabled" :file-list.sync="imgs2" :limit="1" dir-name="/banner" list-type="img"
            @change="setUrl(formData, 'imgs2', 'link')" />
        </el-form-item>
        <el-form-item v-if="formData.type === 'video'" label="视频" prop="video">
          <!-- <Upload :disabled="disabled" :file-list.sync="imgs3" :limit="1" dir-name="/banner" list-type="video"
            @change="setUrl(formData, 'imgs3', 'img')" /> -->
            <el-input v-model="imgs3" placeholder="请输入" @change="setUrl(formData, 'imgs3', 'link')"/>
        </el-form-item>
      </el-col>
    </el-row>
    <el-form-item label="标题" prop="title">
      <el-input v-model="formData.title" placeholder="请输入" maxlength="50" />
    </el-form-item>
    <el-form-item label="描述" prop="des">
      <el-input v-model="formData.des" placeholder="请输入" maxlength="50" />
    </el-form-item>
    <el-form-item label="链接" prop="link" v-if="formData.type === 'link'">
      <el-input v-model="formData.link" placeholder="请输入" />
    </el-form-item>
  </el-form>
</template>

<script>
import Upload from '@/components/Upload'
import FormMixin from "@/views/product/form-mixin.js";
export default {
  name: 'ProductForm',
  mixins: [FormMixin],
  components: { Upload },
  props: {
    /** 类型  14新闻 15下载 16媒体 */
    type: {
      type: String,
      default: '14'
    },
    value: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      imgs: [],
      imgs2: [],
      imgs3: '',
      rules: {
        banner: [{ required: true, message: '请上传', trigger: 'change' }],
        img: [{ required: true, message: '请上传', trigger: 'change' }],
        title: [{  message: '请输入', trigger: 'blur' }],
        des: [{  message: '请输入', trigger: 'blur' }],
        link: [{ message: '请输入', trigger: 'blur' }]
      },
      lst_cate: []
    }
  },
  computed: {
    formData: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    }
  },
  watch: {
    value: {
      handler(val) {
        this.imgs = val.img ? [{ save_url: val.img }] : []
        this.imgs2 = val.link ? [{ save_url: val.link }] : [];
        this.imgs3 =  val.link ;
      },
      immediate: true
    }
  },
  methods: {
    setUrl(row, imgsField, field) {
      if (!this[imgsField].length) return
      if (imgsField == 'imgs3') {
        this.$set(row, 'link', this[imgsField])
      } else { 

        this.$set(row, field || 'url', this[imgsField][0].save_url)
      }
    },
    validate() {
      return this.$refs.form.validate()
    }
  }
}
</script>

<style lang="scss">
.edit-main-box {
  .add-box {
    background-color: #2d82f8;
  }

  .el-icon-circle-plus {
    font-size: 26px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-left: 10px;
    cursor: pointer;
    color: #409eff;
  }
}
</style>
